﻿//
//  (c) Tony Rogerson
//  PINGStance
//      Network Ping
//      SQL Connection stability checking
//      Network latency and performance checking
//
//      Output to a file or local database

namespace PINGStance

module MainModule = 

    open Networking
    open SQLServer
    open BucketFunctions

    open System
    open System.Threading


    [<EntryPoint>]
    let main argv = 
        
        //  Parameters:
        //      {IP/Hostname} 
        //      {Ping Buffer Size} 
        //      {Ping Report at (ms)} 
        //      {Ping Rate (secs)} 
        //      {Log to Console Y/N}
        //      {SQL Connection String}
        //      {SQL Keep Alive Fire (mins)}
        //      {SQL Report at (ms)}
        //      {SQL Rate (secs)}
        //      {Ping Selection - S (SQL) N (PING) B (BOTH)

        let parm_ipaddress = argv.[0]
        let parm_ping_buffer_size = System.UInt16.Parse(argv.[1])
        let parm_ping_report_at_ms = System.UInt16.Parse(argv.[2])
        let parm_ping_rate_sec = System.UInt16.Parse(argv.[3])
        let parm_log_to_console : bool = argv.[4] = "Y"
        let parm_sql_connection = argv.[5]
        let parm_sql_keep_alive_fire = System.UInt16.Parse(argv.[6])
        let parm_sql_report_at_ms = System.UInt16.Parse(argv.[7])
        let parm_sql_rate_sec = System.UInt16.Parse(argv.[8])
        let parm_ping_selection = argv.[9]

        //  Log application start, creates a new log file
        let lock = new ReaderWriterLock()

        write_log_file( "B", (String.Join( " | ", argv )), false, parm_log_to_console, lock )


        //
        //  PING
        //

        if parm_ping_selection = "N" || parm_ping_selection = "B" then
            let pt( parm_ipaddress, 
                    parm_ping_buffer_size, 
                    parm_ping_report_at_ms, 
                    parm_ping_rate_sec, 
                    parm_log_to_console,
                    lock ) = async { PingTest(  parm_ipaddress, 
                                                parm_ping_buffer_size, 
                                                parm_ping_report_at_ms, 
                                                parm_ping_rate_sec, 
                                                parm_log_to_console,
                                                lock   ) |> ignore
                                                    }

            Async.Start( pt( parm_ipaddress, 
                             parm_ping_buffer_size, 
                             parm_ping_report_at_ms, 
                             parm_ping_rate_sec, 
                             parm_log_to_console,
                             lock ) )


        //
        //  SQL
        //
        if parm_ping_selection = "S" || parm_ping_selection = "B" then
            let st ( parm_sql_connection,
                     parm_sql_keep_alive_fire,
                     parm_sql_report_at_ms,
                     parm_sql_rate_sec, 
                     parm_log_to_console,
                     lock ) = async {    SQLTest(   parm_sql_connection,
                                                    int parm_sql_keep_alive_fire,
                                                    int parm_sql_report_at_ms,
                                                    int parm_sql_rate_sec, 
                                                    parm_log_to_console,
                                                    lock ) |> ignore
        
                                                     }

            Async.Start( st(    parm_sql_connection,
                                int parm_sql_keep_alive_fire,
                                int parm_sql_report_at_ms,
                                int parm_sql_rate_sec, 
                                parm_log_to_console,
                                lock ) ) 

        //  
        //  Keep going
        //
        while 1 = 1 do

            Thread.Sleep( 1000 )
         
        0 // return an integer exit code
